﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Hawayel.RFC.Engine.DataStructures;
using System.IO;
using Hawayel.RFC.Engine;

namespace Hawayel.RFC.UI.WinForms
{
    public partial class MainWindow : Form
    {
        /// <summary>
        /// 
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            //.
            _sourcePathLabel.Text = string.Empty;
            _destPathLabel.Text = string.Empty;

            //.

            //.
            _backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(OnBackgroundWorker_ProgressChanged);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            _progressBar.Value = e.ProgressPercentage;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _btnSelectSource_Click(object sender, EventArgs e)
        {
            if (DialogResult.Cancel != _sourceSelector.ShowDialog())
            {
                _sourcePathLabel.Text = _sourceSelector.SelectedPath;
            }
            _sourcePathLabel.Visible = !string.IsNullOrEmpty(_sourcePathLabel.Text);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _btnSelectDest_Click(object sender, EventArgs e)
        {
            if (DialogResult.Cancel != _destSelector.ShowDialog())
            {
                _destPathLabel.Text = _destSelector.SelectedPath;
            }
            _destPathLabel.Visible = !string.IsNullOrEmpty(_destPathLabel.Text);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            //.
            ProcessSettings ps = new ProcessSettings()
            {
                SourceFolder = this._sourceSelector.SelectedPath,
                SearchOption = (this._chkRecursive.Checked) ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly,
                FileTypes = this._fileTypes.Text,

                DestinationFolder = this._destSelector.SelectedPath
            };

            int take = 0;
            if (int.TryParse(this._filesCount.Text, out take))
            {
                ps.MaxFiles = take;
            }

            //.
            Processor p = new Processor() { ReportProgress = _backgroundWorker.ReportProgress };

            var error = p.DoCopy(ps);

            if (ErrorCode.None != error)
            {
                MessageBox.Show(error.ToString());
            }
        }

        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _startCopyBtn_Click(object sender, EventArgs e)
        {
            _backgroundWorker.RunWorkerAsync();
        }

    }
}
